/**
 * Created by Admin on 2018/1/12.
 */
define(function (require, exports, module) {
    var tpl = require("./tpl/statisticalAnalysisTpl.html");
    var loadingTpl = require("../common/tpl/loading.html");
    var template = require("template");
    require("../common/dialog.js");
    require("../common/qy-template");
    require("../common/highchart/highcharts.js");
    require("../common/highchart/linechart.js");
    require("../common/highchart/piechartStatic.js")

    /*获取数据范围列表*/
    var List = {
        getList: function () {
            /*获取区域和数据范围数据*/
            // $.ajax({
            //     url: window.jkconfig.webapiurlv + "analysis/dataType",
            //     success: function (data) {
            //         var dataHtml = "";
            //         if (data.code == 0) {
            //             /*在遍历data.DataTypeInfo,获取数据范围列表*/
            //             if (data.DataTypeInfo.length > 0) {
            //                 for (var i = 0; i < data.DataTypeInfo.length; i++) {
            //                     if (data.DataTypeInfo[i].dataTypeName != "当前有效量") {
            //                         dataHtml += '<option value="' + eval(data.DataTypeInfo[i].dataTypeValue) + '">' + data.DataTypeInfo[i].dataTypeName + '</option>';
            //                     }
            //                 }
            //                 $("[name=dataTypeValue]").append(dataHtml);
            //             }
            //         } else {
            //             $.showErr("获取数据范围列表失败!");
            //         }
            //     }
            // })
            /*获取table数据*/
            ajaxOperator(window.jkconfig.webapiurlv + "analysis/applicantType?dataTypeValue=1");
        }
    }
    List.getList();


    $(function () {
        $("[name=dataTypeValue]").change(function () {
            $("#tradeStatisticalBox").html(loadingTpl);
            ajaxOperator(window.jkconfig.webapiurlv+"analysis/applicantType?dataTypeValue="+$(this).val());
        })
        $("body").on("click", ".btnExport", function () {
            window.location.href = window.jkconfig.webapiurlv + "analysisexport/applicantType?dataTypeValue=" + $("[name=dataTypeValue]").val() + "&" + api_key + "=" + token;
        })
    })

    function ajaxOperator(url) {
        $.ajax({
            url: url,
            success: function (data) {
                if (data.code == 0 && data.info != null) {
                    if (data.info.length < 1) {
                        $(".nodata-title").show();
                        $("#tradeStatisticalBox").hide();
                        var html = '<div class="nodata-title">暂无数据</div>'
                        $("#areaChart1,#areaChart2").html(html);
                    } else {
                        $(".nodata-title").hide();
                        $("#tradeStatisticalBox").show();
                        var html = template.compile(tpl)({data: data});
                        $("#tradeStatisticalBox").html(html);
                        /*扇形图*/
                        var pieApplicantdata = [], piePatData = [];
                        var applicantData = [], patData = [], middleData = [];
                        var colors = ["#C23531", "#2F4554", "#61A0A8", "#D48265", "#91C7AE", "#749F83"];
                        // for (var i = 0; i < data.info.length; i++) {
                        //     middleData.push(data.info[i].applicantPercent,data.info[i].patPercent);
                        // }
                        // if()
                        /*判断合计的applicantPercent不为0和patPercent不为0，才展示图片，否则显示暂无数据*/
                        if (data.info[0].applicantTotal) {
                            for (var i = 1; i < data.info.length; i++) {
                                pieApplicantdata.push({
                                    name: data.info[i].typeName,
                                    y:parseFloat(data.info[i].applicantPercent),
                                    color: colors[i]
                                });
                                piePatData.push({
                                    name: data.info[i].typeName,
                                    y: parseFloat(data.info[i].patPercent),
                                    color: colors[i]
                                });
                            }
                            var newPieChart = new PieChart({//饼图初始化
                                dom: "areaChart1",
                                piedata:pieApplicantdata,
                                pietitle: "权利人类型分析"//饼图一级标题
                            });
                            var newPieChart = new PieChart({//饼图初始化
                                dom: "areaChart2",
                                piedata: piePatData,
                                pietitle: "权利人专利分析",//饼图一级标题
                            });
                        } else {
                            var html = "<div style='width:100%;height:400px;font-size:20px;line-height:400px;text-align:center'>暂无数据</div>"
                            // $(".pt16 .statistical-layout").html(html);
                            $("#areaChart1,#areaChart2").html(html);
                        }
                    }
                }
            }
        })
    }
})